#include <stdio.h>
#include <string.h>
#include "dsmapips.h"
#include "dsmapitd.h"
#include "dsmrc.h"

#define  APLICATION_TYPE "API:Python"


/************************************************
 *                                              *
 * Global VAR                                   *
 *                                              *
 ************************************************/

static dsmInitExIn_t       dsmInitIn;
static dsmInitExOut_t      dsmInitOut;



static dsmApiVersion       apiVersion;


static DataBlk             qData;


static int                 QueryType;

/*
 * Query Filespace
 */
static qryFSData	   queryFSBuffer;

/*
 * Reply Filespace
 */
static qryRespFSData       qRespFSData;


/*
 * Query MC 
 */
static qryMCData           queryMCBuffer;
/*
 * Reply MC
 */
static qryRespMCDetailData qRespMCData;


/*
 * Query Archive
 */

static qryArchiveData	   queryArchiveBuffer;
static dsmObjName 	   objName;


/*
 * Reply Archive
 */
static qryRespArchiveData  qRespArchiveData;





dsInt16_t dsmGetNextQMCObj (dsUint32_t dsmHandle, qryRespMCDetailData *buff)
{
	/*
         * Blanqueo de las estructuras
         */

	memset(&qData, 0, sizeof(DataBlk));
	memset(buff  , 0, sizeof(qryRespMCDetailData)); 	
	/*
         * Version de la estructuras:  qData y qryRespMCDetailData
	 */
	

	qData.stVersion = DataBlkVersion;
	buff->stVersion = qryMCDataVersion;

	qData.bufferLen = sizeof(qryRespMCDetailData);
	qData.numBytes  = 0;
	qData.bufferPtr = (dsmQueryBuff *)buff;

	return dsmGetNextQObj(dsmHandle,&qData);
	
}


dsInt16_t dsmGetNextQArchiveObj (dsUint32_t dsmHandle, qryRespArchiveData *buff)
{
	/*
         * Blanqueo de las estructuras
         */

        memset(&qData, 0, sizeof(DataBlk));
	memset(buff  , 0, sizeof(qryRespArchiveData));
  

	/*
         * Version de la estructuras:  qData y qryRespMCDetailData
         */

	qData.stVersion = DataBlkVersion;
        buff->stVersion = qryRespArchiveDataVersion;

        qData.bufferLen = sizeof(qryRespArchiveData);
        qData.numBytes  = 0;
        qData.bufferPtr = (dsmQueryBuff *)buff;

        return dsmGetNextQObj(dsmHandle,&qData);
}


dsInt16_t dsmGetNextQFSObj(dsUint32_t dsmHandle, qryRespFSData *buff)
{
	/*
         * Blanqueo de las estructuras
         */

        memset(&qData, 0, sizeof(DataBlk));
        memset(buff  , 0, sizeof(qryRespFSData));


        /*
         * Version de la estructuras:  qData y qryRespMCDetailData
         */

	qData.stVersion = DataBlkVersion;

	qData.bufferLen = sizeof(qryRespFSData);
        qData.numBytes  = 0;
        qData.bufferPtr = (dsmQueryBuff *)buff;

        return dsmGetNextQObj(dsmHandle,&qData);

}



dsInt16_t dsmBeginQueryFS (dsUint32_t dsmHanlde, qryFSData *queryBuff)
{
	queryBuff->stVersion = qryFSDataVersion;

	QueryType = qtFS;

	return dsmBeginQuery(dsmHandle, qtFS, (dsmQueryBuff *)queryBuff);
	
}


dsInt16_t dsmBeginQueryMC (dsUint32_t dsmHandle, qryMCData *queryBuff)
{
	queryBuff->stVersion = qryMCDataVersion;

	QueryType = qtMC;

	return dsmBeginQuery(dsmHandle, qtMC, (dsmQueryBuff *)queryBuff);
}



dsInt16_t dsmBeginQueryArchive( dsUint32_t dsmHandle, qryArchiveData *queryBuff)
{
	queryBuff->stVersion = qryArchiveDataVersion;
	
	QueryType = qtArchive;

	return dsmBeginQuery(dsmHandle, qtArchive, (dsmQueryBuff *)queryBuff);
}


dsInt16_t dsmInitExt(dsUint32_t *dsmHandle, char *cNode, char *cOwner, char *cPass, char *cFile, char *options) 
{
	/* 
	 * Inicializa las estructuras de inicio de Session
	 */

        memset(&dsmInitIn , 0, sizeof(dsmInitExIn_t));
        memset(&dsmInitOut, 0, sizeof(dsmInitExOut_t));


        dsmQueryApiVersionEx(&apiVersion);

        dsmInitIn.apiVersionExP    = &apiVer;
        dsmInitIn.clientNodeNameP  = cNode;
        dsmInitIn.clientOwnerNameP = cOwner;
        dsmInitIn.clientPasswordP  = cPass;
        dsmInitIn.applicationTypeP = APLICATION_TYPE; 
        dsmInitIn.configfile       = cFile;
        dsmInitIn.options          = options;
        dsmInitIn.bCrossPlatform   = bFalse;
        dsmInitIn.useUnicode       = bFalse;

        return dsmInitEx(dsmHandle, &dsmInitIn, &dsmInitOut);
}




int main(int n, char *argv[], char *env[])
{
	dsInt16_t rc = 0;
	dsUint32_t dsmHandle = 0;
	dsmApiVersion apiVersion;
	int i = 0;

	dsmQueryApiVersion(&apiVersion);

/*	rc = TSMWrapper_init(&dsmHandle, "HRARCH1P", NULL, "Hrarch1p1"); */
	rc = dsmInitExt(&dsmHandle,"HRARCH1P", NULL, "Hrarch1p1","/opt/tivoli/tsm/client/api/bin/sample/dsm.opt", NULL); 


	if (rc) {
		printf ("Error in dsmInit - Return Code: %d\n", rc);
		return -1;
	}

	
	memset(&queryArchiveBuffer, 0, sizeof(qryArchiveData));
	memset(&objName, 0, sizeof(dsmObjName));

	objName.objType = DSM_OBJ_ANY_TYPE;
	strcpy(objName.fs,"/2008");
	printf ("fs: %s\n", objName.fs);
	strcpy(objName.hl,"/*");
	printf ("hl: %s\n", objName.hl);
	strcpy(objName.ll,"/*");
	printf ("ll: %s\n", objName.ll);
	queryArchiveBuffer.objName = &objName;
	queryArchiveBuffer.owner   = "";
	queryArchiveBuffer.insDateLowerBound.year = DATE_MINUS_INFINITE;
	queryArchiveBuffer.insDateUpperBound.year = DATE_PLUS_INFINITE;
	queryArchiveBuffer.expDateLowerBound.year = DATE_MINUS_INFINITE;
        queryArchiveBuffer.expDateUpperBound.year = DATE_PLUS_INFINITE;

	queryArchiveBuffer.descr= "*";


	rc = dsmBeginQueryArchive(dsmHandle, &queryArchiveBuffer);

	if (rc) {
		printf ("Error in dsmBeginQueryMC - Return Code: %d\n", rc);
                return -1;
	}
	
	rc = dsmGetNextQArchiveObj(dsmHandle,&qRespArchiveData);
	printf ("%d\n", rc);

	while ( rc == DSM_RC_MORE_DATA ) {
	
		printf ("%d, Name: %s\n", i++, qRespArchiveData.objName.ll);
		rc = dsmGetNextQArchiveObj(dsmHandle,&qRespArchiveData);

	} 
	
	if ( rc == DSM_RC_FINISHED ) 
		dsmEndQuery(dsmHandle);
	else {
		printf ("Error in dsmGetNextQMCObj - Return Code: %d\n", rc);
                return -1;
	}	

	return 0;

}

 

